package cn.edu.ljl;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

//登录状态下的搜索经验
///user/search

public class LoginSearchEX extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String key = request.getParameter("experience");

        String sql = "SELECT * FROM `courses` WHERE" + " `course` LIKE '%" + key + "%'" +
                " OR `nam` LIKE '%" + key + "%'";
        System.out.println(sql);


        try {
            List<Experience> experiences = DBMS.getExperience(sql);
            response.setCharacterEncoding("UTF-8");
            Writer writer = response.getWriter();
            StringBuilder sb = new StringBuilder();

            sb.append(Page.getHeader());
            sb.append(toHtml(experiences));
            sb.append(Page.getEnd());
            writer.write(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static String toHtml(List<Experience> experiences) {
        StringBuilder sb = new StringBuilder();
        sb.append("<style type=\"text/css\">\n" +
                "\tbody{\n" +
                "\t\tbackground: url(\"../pic/beijing.jpg\") no-repeat center center fixed;\n" +
                "                /*兼容浏览器版本*/\n" +
                "                -webkit-background-size: cover;\n" +
                "                -o-background-size: cover;\n" +
                "                background-size: cover;\n" +
                "\t}\n" +
                "</style>");


        for (int i = 0; i < experiences.size(); i++) {
            Experience experience = experiences.get(i);

            sb.append("<center>");

            sb.append("<font  face=\"宋体\" size=\"4\" >编号：</font>")
                    .append("<font  face=\"宋体\" size=\"4\" >")
                    .append(experience.getID()).append("</font>");

            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");

            sb.append("<font  face=\"宋体\" size=\"4\" >提供者：</font>")
                    .append("<font  face=\"宋体\" size=\"4\" >")
                    .append(experience.getName()).append("</font>");

            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");

            sb.append("<font  face=\"宋体\" size=\"4\" >科目：</font>")
                    .append("<font  face=\"宋体\" size=\"4\" >")
                    .append(experience.getCourse()).append("</font>");

            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");

            sb.append(String.format("<a href='./EditExperience?id=%s'>修改</a>", experience.getID()));
            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");
            sb.append(String.format("<a href='./deleteSearch?id=%s'>删除</a>", experience.getID()));

            sb.append("<br>");

            sb.append("<font  face=\"宋体\" size=\"4\" color=\"#003153\">建议</font>");
            sb.append("<br>");
            sb.append("<textarea name=\"feel\"  cols=\"40\" rows=\"10\" >")
                    .append(experience.getAdvice()).append("</textarea>");

            sb.append("<br>");
            sb.append("<br>");
        }
        sb.append("</center>");
        return sb.toString();
    }
}
